package com.example.loginproject.view.fragments

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import com.example.loginproject.R
import com.example.loginproject.databinding.FragmentPatternLoginBinding
import com.example.loginproject.models.User
import com.example.loginproject.tool.navigateTo
import com.example.loginproject.user.PasswordType
import com.example.loginproject.view.viewModels.SharedViewModel


class PatternLoginFragment : Fragment() {
    private lateinit var binding:FragmentPatternLoginBinding
    private val viewModel :SharedViewModel by activityViewModels()
    private var hasUser = false
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentPatternLoginBinding.inflate(inflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.unlockView.addDrawFinishedListener { unlockView, s ->
            if(hasUser){
                val user = viewModel.loginedUser.value!!
                viewModel.login(user.name,s,PasswordType.Pattern )
            }
        }
        binding.registerTextView.setOnClickListener {
            navigateTo(PinRegisterFragment())
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.loginedUser.observe(viewLifecycleOwner){
            if(it.name.isEmpty()){
                ChooseUserBottomSheetFragment().show(parentFragmentManager,null)
            }else{
                hasUser = true
            }
        }
    }
}